package com.plate.common.core.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * @ClassName Result
 * @Description 返回对象
 * @Author jiaxd
 * @Date 2023年10月17日 23:17
 * @Version 1.0
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String mesg;

    /**
     * 时间戳
     */
    private Long timestamp;

    /**
     * 返回数据
     */
    private T data;

    public boolean success;


    public Result(ResultCode code) {
        this.code = code.code;
        this.mesg = code.mesg;
        this.success = code.success;
        this.timestamp = (new Date()).getTime();
    }

    public Result(ResultCode code, T data) {
        this.code = code.code;
        this.mesg = code.mesg;
        this.success = code.success;
        this.timestamp = (new Date()).getTime();
        this.data = data;
    }

    public Result(ResultCode code, String mesg) {
        this.code = code.code;
        this.mesg = mesg;
        this.success = code.success;
        this.timestamp = (new Date()).getTime();
    }

    public Result(ResultCode code, String mesg, T data) {
        this.code = code.code;
        this.mesg = mesg;
        this.success = code.success;
        this.timestamp = (new Date()).getTime();
        this.data = data;
    }

    public Result(Integer code, String mesg, T data) {
        this.code = code;
        this.mesg = mesg;
        this.data = data;
    }

    public static <T> Result<T> SUCCESS() {
        return new Result(ResultCode.SUCCESS);
    }

    public static <T> Result<T> SUCCESS(T data) {
        return new Result(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> SUCCESS(String mesg, T data) {
        return new Result(ResultCode.SUCCESS, mesg, data);
    }

    public static <T> Result<T> FAIL() {
        return new Result(ResultCode.FAIL);
    }

    public static <T> Result<T> FAIL(String mesg) {
        return new Result(ResultCode.FAIL, mesg, null);
    }

    public static <T> Result<T> FAIL(ResultCode resultCode) {
        return new Result(resultCode);
    }

    public static <T> Result<T> FAIL(String mesg , T data) {
        return new Result(ResultCode.FAIL, mesg, data);
    }

}
